﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ApportionRuleBLL
    {
        private static readonly IApportionRuleDA ApportionRuleDA = DataAccess.CreateApportionRuleDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public ApportionRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public ApportionRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ApportionRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> OwnerIDs, List<string> CustomerIDs, List<string> SeqNo)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<ApportionRuleInfo> apportionRuleInfos = new List<ApportionRuleInfo>();
                ApportionRuleInfo apportionRuleInfo = null;
                for (i = 0; i < OwnerIDs.Count; i++)
                {
                    apportionRuleInfo = ApportionRuleDA.GetApportionRuleByID(dataBase, tran, OwnerIDs[i], CustomerIDs[i], SeqNo[i], this.dataBaseID[0]);
                    apportionRuleInfos.Add(apportionRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (ApportionRuleDA.DeleteApportionRule(dataBase, tran, OwnerIDs, CustomerIDs, SeqNo, this.dataBaseID) > 0)
                {
                    for (i = 0; i < apportionRuleInfos.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<ApportionRuleInfo>(dataBase, tran, apportionRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "APPORTION_RULE", this.dataBaseID[0]);
                        temp = apportionRuleInfo.OwnerID.ToString() + apportionRuleInfo.CustomerID.ToString() + apportionRuleInfo.SeqNo.ToString();
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return ApportionRuleDA.GetAllApportionRule(this.dataBaseID[0]);
        }

        public ApportionRuleInfo GetInfoByID(string OwnerID, string CustomerID, string SeqNO)
        {
            return ApportionRuleDA.GetApportionRuleByID(OwnerID, CustomerID, SeqNO, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(ApportionRuleQueryEntity apportionRuleQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(apportionRuleQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + apportionRuleQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(apportionRuleQuery.CustomerID))
            {
                sqlWhere.Add(" CUSTOMER_ID " + apportionRuleQuery.CustomerID);
            }
            if (!string.IsNullOrEmpty(apportionRuleQuery.SeqNo))
            {
                sqlWhere.Add(" SEQ_NO " + apportionRuleQuery.SeqNo);
            }
            if (!string.IsNullOrEmpty(apportionRuleQuery.AllowOverShipment))
            {
                sqlWhere.Add(" ALLOW_OVER_SHIPMENT " + apportionRuleQuery.AllowOverShipment);
            }
            if (!string.IsNullOrEmpty(apportionRuleQuery.MaximumPerOver))
            {
                sqlWhere.Add(" MAXIMUM_PER_OVER " + apportionRuleQuery.MaximumPerOver);
            }
            return ApportionRuleDA.GetApportionRuleByQueryList(sqlWhere, apportionRuleQuery, this.dataBaseID[0]);
        }

        public List<ApportionRuleInfo> GetInfoListByID(string OwnerID)
        {
            return ApportionRuleDA.GetApportionRuleListByID(OwnerID, this.dataBaseID[0]);
        }

        public string Insert(ApportionRuleInfo apportionRuleInfo)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (apportionRuleInfo != null)
                {
                    apportionRuleInfo.UpdatedBy = apportionRuleInfo.CreatedBy;
                    apportionRuleInfo.CreatedDate = DateTime.Now;
                    apportionRuleInfo.UpdatedDate = DateTime.Now;
                }
                if (ApportionRuleDA.InsertApportionRule(dataBase, tran, apportionRuleInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<ApportionRuleInfo>(null, apportionRuleInfo, AuditAction.Insert, this.pubParasInfo.UserID, "APPORTION_RULE", this.dataBaseID[0]);
                    temp = apportionRuleInfo.OwnerID.ToString() + apportionRuleInfo.CustomerID.ToString() + apportionRuleInfo.SeqNo.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string Update(ApportionRuleInfo apportionRuleInfo)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                apportionRuleInfo.UpdatedDate = DateTime.Now;
                apportionRuleInfo.UpdatedBy = this.pubParasInfo.UserID;
                ApportionRuleInfo oldApportionRuleInfo = ApportionRuleDA.GetApportionRuleByID(dataBase, tran, apportionRuleInfo.OwnerID, apportionRuleInfo.CustomerID, apportionRuleInfo.SeqNo.ToString(), this.dataBaseID[0]);
                if (ApportionRuleDA.UpdateApportionRule(dataBase, tran, apportionRuleInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<ApportionRuleInfo>(oldApportionRuleInfo, apportionRuleInfo, AuditAction.Update, this.pubParasInfo.UserID, "APPORTION_RULE", this.dataBaseID[0]);
                    temp = apportionRuleInfo.OwnerID.ToString() + apportionRuleInfo.CustomerID.ToString() + apportionRuleInfo.SeqNo.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }
    }
}

